<?php
namespace app\controller\model;

use app\BaseController;
use think\facade\Db;

class Users extends BaseController
{
    protected $tables;
    public function __construct(){
        $this->tables=new Tables();
    }
    /**
     * @param $userID 用户ID
     * @param $amount 金额
     * @param $payWay 支付方式，0微信，1余额，100系统
     * @param $orderID 订单号
     * @param $payNumbers 相关支付订单号 只有微信支付有
     * @return false|void
     * @throws \think\db\exception\DbException
     */
    public function userAddAmount($userID=0,$amount=0,$payWay=0,$orderID=0,$payNumbers=''){
        //用户增减金额
        lock();
        $user = r_one($this->tables->users,"id=$userID");
        if(!$user){
            unlockRollback();
            return false;
        }
        if($payWay==1){
            $up = DB::name($this->tables->users)->where("id",$userID)->inc("amount",$amount)->update();
            $endAmount = bcadd($user['amount'],$amount,2);
        }else{
            $up=true;
            $endAmount = $user['amount'];
        }
        if($up){
            //写日志
            $orderTitle='';
            $price=0;
            $number=0;
            if($orderID>0){
                $order = r_one($this->tables->orders,"id=$orderID");
                if($order){
                    $orderTitle=$order['goodsTitle'];
                    $price=$order['price'];
                }
            }
            $logArr = ['uid'=>$userID,'payWay'=>$payWay,'startAmount'=>$user['amount'],'endAmount'=>$endAmount,'payNumbers'=>$payNumbers,'orderID'=>$orderID,'orderTitle'=>$orderTitle,'price'=>$price,'number'=>$number,'amount'=>$amount,'addTime'=>nowTime()];
            $log = DB::name($this->tables->userAmountLog)->insert($logArr);
            if(!$log){
                unlockRollback();
                return false;
            }
            unlockCommit();
            return true;
        }
    }
    public function setLevel($user=[]){
        if(!$user || !is_array($user)){return false;}
        $userID = $user['id'];
        if($user['level']==1){
            $countUser = DB::name($this->tables->users)->where("pathid",$userID)->where("level",">",0)->count();
            if($countUser>=2){
                DB::name($this->tables->users)->where("id",$userID)->update(['level'=>2,'levelStr'=>'合伙人']); 
                //升级合伙人成功，拆分3个会员给上级
                if($user['pathid']>0){
                    $pathID=$user['pathid'];
                    $pathUser = r_one($this->tables->users,"id=$pathID");
                    if($pathUser && $pathUser['level']>=2){
                        DB::name($this->tables->users)->where("pathid",$userID)->limit(0,2)->update(['pathid'=>$pathID]);
                    }
                }
                return true;
            }
        }
        if($user['level']==0){
            $countOrder = DB::name($this->tables->orders)->where("status",">",0)->where("afterSellStatus",0)->count();
            if($countOrder>0){
                DB::name($this->tables->users)->where("id",$userID)->update(['level'=>1,'levelStr'=>'会员']);
                //升级正式会员成功 产看上级是否需要升级
                if($user['pathid']>0){
                    $pathID=$user['pathid'];
                    $pathUser = r_one($this->tables->users,"id=$pathID");
                    if($pathUser && $pathUser['level']==1){
                        $this->setLevel($pathUser);
                    }
                }
                return true;
            }
        }
        return false;
    }
}
?>